package com.accp.reporter.controller;

import com.accp.reporter.pojo.Draft;
import com.accp.reporter.repository.DraftRepository;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.apache.commons.lang.math.RandomUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.UUID;


@Controller
@RequestMapping("/tougao")
public class DraftConntroller {

    @Autowired
    private DraftRepository draftRepository;
    //用于跳转添加页面
    @RequestMapping("/success")
    public String inadd() {
        return "success";
    }

    //添加稿件
    @RequestMapping(value = "/addsubmission", method= RequestMethod.POST)
    public String addUserSave(Draft user, HttpServletRequest request,
                         @RequestParam(value = "a_idPicPath", required = false) MultipartFile attach)throws Exception {
            String title = request.getParameter("title");
            String username = request.getParameter("username");
            String draftClass = request.getParameter("draftClass");
            String panduan = null;
            if(draftClass.equals("1")){
                panduan="消息";
            }
            if(draftClass.equals("2")){
                panduan="通讯";
            }
            if(draftClass.equals("3")){
                panduan="评论";
            }
            if(draftClass.equals("4")){
                panduan="专访";
            }
            /*String draftType = request.getParameter("draftType");*/

        String idPicPath = null;
        // 判断文件是否为空
        if (!attach.isEmpty()) {
            // 要把文件上传到的位置
            String path = request.getSession().getServletContext()
                    .getRealPath("statics"+ UUID.randomUUID() + File.separator + "uploadfiles");
            String oldFileName = attach.getOriginalFilename();// 原文件名
            String prefix = FilenameUtils.getExtension(oldFileName);// 原文件后缀
            int filesize = 500000;
            if (attach.getSize() > filesize) {// 上传大小不得超过 500k
                request.setAttribute("uploadFileError", " * 上传大小不得超过 500k");
                return "/error";
            } else if (prefix.equalsIgnoreCase("jpg") || prefix.equalsIgnoreCase("png")
                    || prefix.equalsIgnoreCase("jpeg") || prefix.equalsIgnoreCase("pneg")|| prefix.equalsIgnoreCase("mp3")
                    || prefix.equalsIgnoreCase("mp4")) {// 上传文件格式不正确
                String fileName = System.currentTimeMillis() + RandomUtils.nextInt(1000000) + "_Personal.jpg";
                File targetFile = new File(path, fileName);
                if (!targetFile.exists()) {
                    targetFile.mkdirs();
                }
                // 保存
                try {
                    attach.transferTo(targetFile);
                } catch (Exception e) {
                    e.printStackTrace();
                    request.setAttribute("uploadFileError", " * 上传失败！");
                    return "error";
                }
                idPicPath = path + File.separator + fileName;
                System.out.println("==========path:" + idPicPath + "==========");
            } else {
                request.setAttribute("uploadFileError", " * 上传图片格式不正确");
                return "error";
            }
        }
        String content = request.getParameter("content");
        int i = draftRepository.insert(new Draft(title,username,panduan,idPicPath,content));

            return "redirect:success";
    }
}
